package com.example.mvvm.activity;

import android.os.Bundle;
import android.os.PersistableBundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.Observer;

import com.example.mvvm.BR;
import com.example.mvvm.R;
import com.example.mvvm.adapter.MainAdapter;
import com.example.mvvm.base.BaseActivity;
import com.example.mvvm.base.livedata.MessageLiveEvent;
import com.example.mvvm.base.livedata.StatusLiveEvent;
import com.example.mvvm.databinding.ActivityMainBinding;
import com.example.mvvm.fragment.ChatsFragment;
import com.example.mvvm.fragment.DiscoverFragment;
import com.example.mvvm.fragment.HomeFragment;
import com.example.mvvm.fragment.MeFragment;
import com.example.mvvm.viewmodel.MainViewModel;

import java.util.ArrayList;

public class MainActivity extends BaseActivity<MainViewModel, ActivityMainBinding> {


    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        MainAdapter adapter = new MainAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(HomeFragment.newInstance());
        fragments.add(ChatsFragment.newInstance());
        fragments.add(DiscoverFragment.newInstance());
        fragments.add(MeFragment.newInstance());
        adapter.setFragments(fragments);
        getBinding().mainPager.setAdapter(adapter);
        getBinding().mainPager.setSlidingEnable(false);

        getViewModel().getLiveData().observe(this, currentIndex -> {
            getBinding().mainPager.setCurrentItem(currentIndex);
        });
    }

    @Override
    public int variableId() {
        return BR.viewModel;
    }


    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        outState.clear();
        super.onSaveInstanceState(outState);
    }
}